package SubServer;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import General.SubServerServiceInterface;

public class SubServer {
	
	private String fRegistryIp;
	private String fHost;
	private int fRegistryPort;
	
	public SubServer(String host, String registryIp, int registryPort) {
		fRegistryIp = registryIp;
		fHost = host;
		fRegistryPort = registryPort;
	}
	
	public void start() {
		try {
			// Look up registry
			Registry registry = LocateRegistry.getRegistry(fRegistryIp, fRegistryPort);
			
			// Look up SubServerService
			SubServerServiceInterface subServerRegistry = (SubServerServiceInterface)registry.lookup("SubServerService");
			
			// Output
			System.out.println("[SUBSERVER] Subserver started on host " + fHost);
			
			// Register ShipQuoter
			ShipQuoterService shipQuoter = new ShipQuoterService();
			if (subServerRegistry.register(shipQuoter)) {
				System.out.println("[SUBSERVER] Subserver connected to main server");
			} else {
				System.out.println("[SUBSERVER] Connection to main server refused.");
			}
			
		} catch (NotBoundException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
}
